package org.brownflat.watergate.domain;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Date;

@MappedSuperclass
@SuppressWarnings("serial")
public abstract class BaseEntity implements Identifiable, Serializable {

    public static final int INITIAL_NON_ZERO_ODD_NUMBER = 17;
    public static final int MULTIPLIER_NON_ZERO_ODD_NUMBER = 37;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "updated_at")
    private Date updatedAt = new Date();

    @Column(name = "created_at")
    private Date createdAt = new Date();

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }

    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(INITIAL_NON_ZERO_ODD_NUMBER, MULTIPLIER_NON_ZERO_ODD_NUMBER).append(getId()).append(getId()).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
       if (obj == null) { return false; }
       if (obj == this) { return true; }
       if (obj.getClass() != getClass()) {
         return false;
       }
       BaseEntity rhs = (BaseEntity) obj;
       return new EqualsBuilder().append(getId(), rhs.getId()).append(getId(), rhs.getId()).isEquals();
    }

    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
